#include <vector>
#include <iostream>

using namespace std;

class CFenvikTree
{
public:
	CFenvikTree( int nn ): n( nn ) { t.assign( n, 0 ); };
	CFenvikTree( vector< int > a );
	int Sum( int r );
	void Inc( int i, int delta );
	int Sum( int l, int r ) { if( l <= 0 ) exit( 1 ); return Sum( r ) - Sum( l - 1 ); };
	void Set( int i, int newz );
private:
	vector< int > t;
	int n;
};

void CFenvikTree::Set( int i, int newz ) {
	if( i >= t.size() ) {
		printf( "error, Invalid num\n");
		exit( 1 );
	}
	if( i < 0 ) {
		printf( "Wrong using of Fenvik, too bad\n" );
	};
	int old = t[i];
	Inc( i, newz - old );
};


int CFenvikTree::Sum( int r ) {
	if( r >= t.size() ) {
		printf( "error, Invalid num\n");
		exit( 1 );
	}
	if( r < 0 ) {
		printf( "Wrong using of Fenvik, too bad\n" );
	};
	int result = 0;
	while( r >= 0 ) {
		result += t[r];
		r = ( r & ( r + 1 ) ) - 1;
	}
	return result;
};

void CFenvikTree::Inc( int i, int delta ) {
	while( i < n ) {
		t[i] += delta;
		i = i | ( i + 1 );
	}
};

CFenvikTree::CFenvikTree( vector< int > a ) {
	n = a.size();
	t.assign( n, 0 );
	for( unsigned i = 0; i < a.size(); i++ )
		Inc( i, a[i] );
};


int main() {
	vector< int > a;
	for( int i = 0; i < 17; i++ ) {
		a.push_back( i );
	}
	CFenvikTree tree( a );
	tree.Set( 1, -5 );
	printf( "%d\n", tree.Sum( 2 ) );
	return 0;
};